<script>

import * as echarts from 'echarts';
// import $ from 'jquery'
export default {
  name: 'VisualizationFive',
  data() {
    return{

    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      this.$axios.get('/api/vis/data5', {}).then(res => {
        if (res.data.code === 20041) {
          let data1 = [];
          let data2 = [];
          for (let i = 0; i < 10; i++) {
            data1.push(
                res.data.data[i].param1
            );
            data2.push(
                res.data.data[i].param2
            );
          }
          this.init(data1, data2)
        }
      }).catch(error => {
        this.open4(error);
      })
    },
    open4(msg) {
      this.$notify.error({
        title: msg,
        message: '这是一条错误的提示消息'
      });
    },
    init(data1, data2) {
      var chartDom = document.getElementById('main5');
      var myChart = echarts.init(chartDom);
      var option;

      option = {
        title: [
          {
            text: '热门商品售量Top10'
          }
        ],
        dataZoom: [{
          type: 'slider',
          show: false,
          startValue: 0,
          endValue: 5,
          height: '15',
          bottom: '3%',
          zoomLock: true,
          brushSelect: false,
        }],
        tooltip: {
          trigger: 'axis',
          valueFormatter: (value) => value + '次' //改变显示文字
        },
        // legend: {
        //    data: ['Rainfall']
        // },
        toolbox: {
          show: true,
          feature: {
            dataView: { show: true, readOnly: false },
            magicType: { show: true, type: ['line', 'bar'] },
            restore: { show: true },
            saveAsImage: { show: true }
          }
        },
        calculable: true,
        xAxis: [
          {
            name: "热门商品",
            type: 'category',
            // prettier-ignore
            data: data1
          }
        ],
        yAxis: [
          {
            name : "销售量",
            type: 'value'
          }
        ],
        series: [
          {
            // name: 'Rainfall',
            type: 'bar',
            data: data2,
            markPoint: {
              data: [
                { type: 'max', name: 'Max' },
                { type: 'min', name: 'Min' }
              ]
            },
            markLine: {
              data: [{ type: 'average', name: 'Avg' }]
            }
          }
        ]
      };

      setInterval(function () {
        // 每次向后滚动一个，最后一个从头开始。
        if (option.dataZoom[0].endValue == option.xAxis[0].data.length) {
          option.dataZoom[0].endValue = 6;
          option.dataZoom[0].startValue = 0;
        }
        else {
          option.dataZoom[0].endValue = option.dataZoom[0].endValue + 1;
          option.dataZoom[0].startValue = option.dataZoom[0].startValue + 1;
        }
        myChart.setOption(option);

      }, 2000);

      option && myChart.setOption(option);
    }
  }

}
</script>

<template>
  <div id="main5"></div>
</template>

<style scoped>
#main5 {
  width: 100%;
  height: 100%;
}
</style>